import { getNotReadMessage } from '@/api/message'

const state = {
	id: '',
	avatar: '',
	username: '',
	name: '',
	create_time: '',
	roleId: '',
	sex: '',
	deptId: '',
	dept: '',
	majorId: '',
	major: '',
	clazz: '',
	teacherId: '',
	teacher: '',
	birth: '',
	nation: '',
	position: '',
	area: '',
	address: '',
	password: '',
	nickname: '',
	phone: '',
	grade: '',
	email: ''
}
const mutations = {
	SET_ID: (state, data) => {
		state.id = data
	},
	SET_USERNAME: (state, data) => {
		state.username = data
	},
	SET_AVATAR: (state, data) => {
		state.avatar = data
	},
	SET_NAME: (state, data) => {
		state.name = data
	},
	SET_CREATETIME: (state, data) => {
		state.create_time = data
	},
	SET_ROLEID: (state, data) => {
		state.roleId = data
	},
	SET_SEX: (state, data) => {
		state.sex = data
	},
	SET_DEPTID: (state, data) => {
		state.deptId = data
	},
	SET_DEPT: (state, data) => {
		state.dept = data
	},
	SET_MAJORID: (state, data) => {
		state.majorId = data
	},
	SET_MAJOR: (state, data) => {
		state.major = data
	},
	SET_CLAZZ: (state, data) => {
		state.clazz = data
	},
	SET_TEACHERID: (state, data) => {
		state.teacherId = data
	},
	SET_TEACHER: (state, data) => {
		state.teacher = data
	},
	SET_BIRTH: (state, data) => {
		state.birth = data
	},
	SET_NATION: (state, data) => {
		state.nation = data
	},
	SET_POSITION: (state, data) => {
		state.position = data
	},
	SET_AREA: (state, data) => {
		state.area = data
	},
	SET_ADDRESS: (state, data) => {
		state.address = data
	},
	SET_PASSWORD: (state, data) => {
		state.password = data
	},
	SET_NITCKNAME: (state, data) => {
		state.nickname = data
	},
	SET_PHONE: (state, data) => {
		state.phone = data
	},
	SET_GRADE: (state, data) => {
		state.grade = data
	},
	SET_EMAIL: (state, data) => {
		state.email = data
	},
	SET_NICKNAME: (state, data) => {
		state.nickname = data
	}
}
const actions = {
	storeUserInfo: ({ commit }, data) => {
		uni.setStorage({
			key: "vuex",
			data: JSON.stringify(data)
		})
		const { 
			id,
			username,
			role_id: roleId,
			create_time,
			avatar,
			name,
			sex,
			dept_id: deptId,
			dept,
			major_id: majorId,
			major,
			clazz,
			teacher_id: teacherId,
			teacher,
			birth,
			nation,
			position,
			area,
			address,
			password,
			nickname,
			phone,
			grade,
			email
		} = data
		commit('SET_ID', id)
		commit('SET_USERNAME', username)
		commit('SET_AVATAR', avatar)
		commit('SET_NAME', name)
		commit('SET_ROLEID', roleId)
		commit('SET_CREATETIME', create_time)
		commit('SET_SEX', sex)
		commit('SET_DEPTID', deptId)
		commit('SET_DEPT', dept)
		commit('SET_MAJORID', majorId)
		commit('SET_MAJOR', major)
		commit('SET_CLAZZ', clazz)
		commit('SET_TEACHERID', teacherId)
		commit('SET_TEACHER', teacher)
		commit('SET_BIRTH', birth)
		commit('SET_NATION', nation)
		commit('SET_POSITION', position)
		commit('SET_AREA', area)
		commit('SET_ADDRESS', address)
		commit('SET_PASSWORD', password)
		commit('SET_NITCKNAME', nickname)
		commit('SET_PHONE', phone)
		commit('SET_GRADE', grade)
		commit('SET_EMAIL', email)
		getNotReadMessage(id).then(res => {
			const { code, data } = res
			if(code === 200) {
				commit('UPDATE_MESSAGE', data)
			}
		})
	}
}
const user = {
	state,
	mutations,
	actions
}

export default user